You can find the slides (and additional materials) here: https://github.com/jstbcs/ws-bayesian-stats-r.
You can find the slides (and additional materials) here: https://github.com/jstbcs/ws-bayesian-stats-r.
Julia Haaf
Assistant Professor at the Psychological Methods Department, University of Amsterdam
email: j.m.haaf@uva.nl
Job alert: Two temporary assistant professor positions at the Psychological Methods Unit (https://www.academictransfer.com/nl/297540/assistant-professor-of-psychological-methods/).
BayesFactor packageExample from Rouder, Haaf, & Aust (2018)
generalTestBF, lmBFregressionBF, lmBFcorrelationBFttestBFmeta.ttestBFanovaBF, lmBFcontingencyTableBFproportionBFgeneralTestBF, lmBFregressionBF, lmBFcorrelationBFttestBFmeta.ttestBFanovaBF, lmBFcontingencyTableBFproportionBFCan be used to test our position 1: The media story increases favorability ratings of refugees.
BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study)
If you want to understand formulas in R better try this tutorial (for general setup) and this tutorial (for mixed models).
Obtain strong evidence by increasing model discriminability.
Stefan, Gronau, Schönbrodt, & Wagenmakers (2019)
BayesFactor package in R and JASP use default priors for the t-test called JZS prior.BayesFactor package in R and JASP use default priors for the t-test called JZS prior.BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study
, rscale = 1 / sqrt(2))
BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study
, rscale = 1 / sqrt(2))
par(mar = c(3,3,.5,.5), mgp = c(2,.7,0))
x <- seq(-3, 3, .01)
y <- dcauchy(x, 0, 1/sqrt(2))
plot(x, y, type = "l", lwd = 2
, ylab = "Density", xlab = "Effect Size"
, col = "darkblue", ylim = c(0, .5))
lines(x, dnorm(x), col = adjustcolor(1, .5), lty = 2, lwd = 2)
abline(v = c(-1, 0, 1), lwd = 0.5, col = adjustcolor(1, alpha.f = 0.3))
sim.pred.fun <- function(scale){
delta <- rcauchy(1, location = 0, scale = scale)
mu <- runif(1, -1000, 1000)
s2 <- MCMCpack::rinvgamma(1, 0.5, 0.5)
I <- 100
cond <- rep(c(-1/sqrt(2), 1/sqrt(2)), each = I)
y <- rnorm(2 * I, mu + cond * delta * sqrt(s2), sqrt(s2))
return(diff(tapply(y, cond, mean)))
}
res <- replicate(100000, sim.pred.fun(1/sqrt(2)))
round(quantile(res), 2)
## 0% 25% 50% 75% 100% ## -1179562.46 -1.74 -0.01 1.70 155159.42
BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study
, rscale = 1 / 4)
Put your setting in the zoom chat!
BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study
, rscale = 1 / 2)
## Bayes factor analysis ## -------------- ## [1] Alt., r=0.5 : 2.142883 ±0% ## ## Against denominator: ## Null, mu1-mu2 = 0 ## --- ## Bayes factor type: BFindepSample, JZS
The plight of refugees generates an empathic response which results in an increase in favorability for conservative and liberals alike.
est.attitude <- BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study, rscale = 1 / 2
, posterior = T, iterations = 20000)
est.attitude <- BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study, rscale = 1 / 2
, posterior = T, iterations = 20000)
plot(est.attitude[,"beta (Neutral - Refugee plight)"])
The plight of refugees generates an empathic response which results in an increase in favorability for conservative and liberals alike.
BayesFactor.BayesFactor.BayesFactor::ttestBF(formula = attitude ~ story
, data = media_study
, rscale = 1 / 2
, nullInterval = c(-Inf, 0))
## Bayes factor analysis ## -------------- ## [1] Alt., r=0.5 -Inf<d<0 : 4.196947 ±0% ## [2] Alt., r=0.5 !(-Inf<d<0) : 0.08881904 ±0% ## ## Against denominator: ## Null, mu1-mu2 = 0 ## --- ## Bayes factor type: BFindepSample, JZS
SourceURL <- "https://raw.githubusercontent.com/PerceptionCognitionLab/data0/master /contexteffects/FlankerStroopSimon/cleaning.R" devtools::source_url(SourceURL)
library(tidyverse) stroop.agg <- stroop %>% group_by(ID, congruency) %>% summarize(mrt = mean(RT)) %>% spread(congruency, mrt)
Figure from Rouder, Engelhardt, McCabe, & Morey (2016)
media_study$story <- factor(media_study$story)
media_study$political_affiliation <- factor(media_study$political_affiliation)
BayesFactor::anovaBF(formula = attitude ~ story * political_affiliation
, data = media_study)
rscaleEffects= A named vector of prior settings for individual factors, overridingrscaleFixedandrscaleRandom.Values are scales, names are factor names.
BayesFactor::anovaBF(formula = attitude ~ story * political_affiliation
, data = media_study
, rscaleEffects = c("story" = 1/2
, "political_affiliation" = 1/2
, "story:political_affiliation" = 1/3))
BayesFactor::anovaBF(formula = attitude ~ story * political_affiliation
, data = media_study
, rscaleEffects = c("story" = 1/2
, "political_affiliation" = 1/2
, "story:political_affiliation" = 1/3))
## Bayes factor analysis ## -------------- ## [1] story : 1.835452 ±0% ## [2] political_affiliation : 191.5876 ±0% ## [3] story + political_affiliation : 468.3391 ±0.73% ## [4] story + political_affiliation + story:political_affiliation : 1156.833 ±7.65% ## ## Against denominator: ## Intercept only ## --- ## Bayes factor type: BFlinearModel, JZS
Explore the range of settings using all combinations.
| a | b |
|---|---|
| 0.3 | 0.3 |
| 0.5 | 0.3 |
| 0.7 | 0.3 |
| 0.3 | 0.5 |
| 0.5 | 0.5 |
| 0.7 | 0.5 |
| 0.3 | 0.7 |
| 0.5 | 0.7 |
| 0.7 | 0.7 |
dat_media.csv.geturl <-"https://raw.githubusercontent.com/jstbcs/ws-bayesian-stats-r/ main/data/dat_media.csv" media_study <- read.csv(geturl)
\({\cal M}_2:\) The effect of the refugee media story is positive for liberals and negative for conservatives.
\({\cal M}_2:\) The effect of the refugee media story is positive for liberals and negative for conservatives.
Encompassing approach (Klugkist, Laudy, & Hoijtink, 2005):
\[ \begin{align} Y_{RL} - Y_{CL} &= 2\theta_1 + 2\theta_3\\ Y_{RC} - Y_{CC} &= 2\theta_1 - 2\theta_3\\ \end{align} \]
M <- 100000 theta1 <- rcauchy(M) theta3 <- rcauchy(M) mean(theta1 + theta3 > 0 & theta1 - theta3 < 0)
## [1] 0.24943
tmp <- BayesFactor::anovaBF(formula = attitude ~ story * political_affiliation
, data = media_study
, rscaleEffects = c("story" = 1/2
, "political_affiliation" = 1/2
, "story:political_affiliation" = 1/3))
est <- BayesFactor::posterior(model = tmp
, iterations = 20000
, index = 4)
Reconstructing cell means and effects from the estimates.
Y_cl <- est[, "story-Neutral"] + est[, "story:political_affiliation-Neutral.&.Liberals"] Y_rl <- est[, "story-Refugee plight"] + est[, "story:political_affiliation-Refugee plight.&.Liberals"] Y_cc <- est[, "story-Neutral"] + est[, "story:political_affiliation-Neutral.&.Conservatives"] Y_rc <- est[, "story-Refugee plight"] + est[, "story:political_affiliation-Refugee plight.&.Conservatives"]
(postprob <- mean(Y_rl - Y_cl > 0 & Y_rc - Y_cc < 0))
## [1] 0.36585
Bayes factor:
(BF_u3 <- 0.25 / postprob)
## [1] 0.6833402
bftab <- tmp@bayesFactor[, 1:2] bftab[, 1] <- exp(bftab[, 1]) bftab[5, 1] <- bftab[4, 1] / BF_u3 rownames(bftab)[5] <- "Position 3" knitr::kable(bftab)
| bf | error | |
|---|---|---|
| story | 1.835452 | 0.0000000 |
| political_affiliation | 191.587618 | 0.0000000 |
| story + political_affiliation | 464.094900 | 0.0202599 |
| story + political_affiliation + story:political_affiliation | 1058.549700 | 0.0086121 |
| Position 3 | 1549.081631 | NA |
generalTestBF, lmBFregressionBF, lmBFcorrelationBFttestBFmeta.ttestBFanovaBF, lmBFcontingencyTableBFproportionBFThis function computes Bayes factors corresponding to restrictions on a full model.
data(puzzles)
BayesFactor::generalTestBF(RT ~ shape * color + ID
, data = puzzles
, whichRandom = "ID")
## Bayes factor analysis ## -------------- ## [1] shape : 0.6114754 ±0.01% ## [2] color : 0.6114754 ±0.01% ## [3] shape + color : 0.3865341 ±0.82% ## [4] shape + color + shape:color : 0.1328641 ±1.55% ## [5] ID : 111516.6 ±0% ## [6] shape + ID : 316107.6 ±0.98% ## [7] color + ID : 313479.8 ±1.41% ## [8] shape + color + ID : 1309516 ±1.67% ## [9] shape + color + shape:color + ID : 512538.7 ±1.99% ## ## Against denominator: ## Intercept only ## --- ## Bayes factor type: BFlinearModel, JZS
This function computes Bayes factors corresponding to restrictions on a full model.
This function computes Bayes factors, or samples from the posterior, of specific linear models (either ANOVA or regression).
BayesFactor::lmBF(RT ~ shape + color + shape:color + ID
, data = puzzles
, whichRandom = "ID")
## Bayes factor analysis ## -------------- ## [1] shape + color + shape:color + ID : 488891.2 ±3% ## ## Against denominator: ## Intercept only ## --- ## Bayes factor type: BFlinearModel, JZS
Rouder & Morey (2012)
dat_media using generalTestBF and lmBF.bugs.csv.BayesFactor can do a lot of conventional tests, but in Bayesian.Cook, J., & Lewandowsky, S. (2016). Rational irrationality: Modeling climate change belief polarization using Bayesian networks. Topics in Cognitive Science, 8(1), 160–179. Retrieved from https://dx.doi.org/10.1111/tops.12186
Klugkist, I., Laudy, O., & Hoijtink, H. (2005). Inequality constrained analysis of variance: A bayesian approach. Psychological Methods, 10(4), 477.
Rouder, J. N., Engelhardt, C. R., McCabe, S., & Morey, R. D. (2016). Model comparison in anova. Psychonomic Bulletin & Review, 23(6), 1779–1786. Retrieved from https://doi.org/10.3758/s13423-016-1026-5
Rouder, J. N., Haaf, J. M., & Aust, F. (2018). From theories to models to predictions: A Bayesian model comparison approach. Communication Monographs, 85, 41–56. Retrieved from https://doi.org/10.1080/03637751.2017.1394581
Rouder, J. N., & Morey, R. D. (2012). Default Bayes factors for model selection in regression. Multivariate Behavioral Research, 47, 877–903. Retrieved from http://dx.doi.org/10.1080/00273171.2012.734737
Rouder, J. N., Morey, R. D., Speckman, P. L., & Province, J. M. (2012). Default Bayes factors for ANOVA designs. Journal of Mathematical Psychology, 56, 356–374. Retrieved from http://dx.doi.org/10.1016/j.jmp.2012.08.001
Rouder, J. N., Speckman, P. L., Sun, D., Morey, R. D., & Iverson, G. (2009). Bayesian \(t\)-tests for accepting and rejecting the null hypothesis. Psychonomic Bulletin and Review, 16, 225–237. Retrieved from http://dx.doi.org/10.3758/PBR.16.2.225
Stefan, A. M., Gronau, Q. F., Schönbrodt, F. D., & Wagenmakers, E.-J. (2019). A tutorial on Bayes Factor Design Analysis using an informed prior. Behavior Research Methods, 51(3), 1042–1058. doi:10.3758/s13428-018-01189-8
Tversky, A., & Kahneman, D. (1974). Judgment under uncertainty: Heuristics and biases. Science, 185(4157), 1124–1131. Retrieved from https://doi.org/10.1126/science.185.4157.1124